<?php
/**
 * Created by PhpStorm
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Types extends Base {

    /**
     * 资讯分类
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $user_name = trim(input('user_name'), ' ');
            $where = [];
            if(isset($user_name) && $user_name != ''){
                $where['t_name'] = ['like', '%' . $user_name . '%'];
            }

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_types')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_types')
                ->where($where)
                ->order('t_sort desc,t_time asc')
                ->page($NowPage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function typeAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                't_name' => $t_name,
                't_thumb' => $t_thumb,
                't_remark' => $t_remark,
                't_sort' => $t_sort,
                't_time' => date('Y-m-d H:i:s'),
            ];
            $str = trim($t_name,' ');
            $str= iconv("UTF-8","gb2312//IGNORE", $str);
            $tmp = bin2hex(substr($str,0,1));
            if($tmp>='B0'){ //汉字的开始
                $t=$this->getLetter(hexdec(bin2hex(substr($str,0,2))));
                $letter = chr($t);
            }else{
                $letter = strtoupper(substr($str,0,1));
            }
            $letter = $this->getLetter2($letter,mb_substr(trim($t_name,' '),0,1));
            $data['t_letter'] = $letter;
            $res = Db::table('up_types')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('types/typeadd');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function typeEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                't_name' => $t_name,
                't_thumb' => $t_thumb,
                't_remark' => $t_remark,
                't_sort' => $t_sort,
                't_time' => date('Y-m-d H:i:s'),
            ];
            $str = trim($t_name,' ');
            $str= iconv("UTF-8","gb2312//IGNORE", $str);
            $tmp = bin2hex(substr($str,0,1));
            if($tmp>='B0'){ //汉字的开始
                $t=$this->getLetter(hexdec(bin2hex(substr($str,0,2))));
                $letter = chr($t);
            }else{
                $letter = strtoupper(substr($str,0,1));
            }
            $letter = $this->getLetter2($letter,mb_substr(trim($t_name,' '),0,1));
            $data['t_letter'] = $letter;
            $res = Db::table('up_types')->where('t_id',$t_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $t_id = input('t_id');
        $info = Db::name('types')
            ->where('t_id',$t_id)
            ->find();
        $this->assign([
            'info' => $info,
        ]);
        return $this->fetch('types/typeedit');
    }
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            't_sort'=>input('value'),
        ];
        $res = Db::name('types')->where('t_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function typeDel(){
        $nt_id = input('id');
        $have = Db::table('up_pp')->where('pp_t_id',$nt_id)->find();
        if($have){
            return json(['code' => 100, 'msg' => '此分类下有品牌，不能删除，请先更改品牌所属']);
        }
        $res = Db::table('up_types')->where('t_id',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     *
     * @return \think\response\Json
     */
    public function upup(){
        $new_id = input('id');
        $res = Db::table('up_types')->where('t_id',$new_id)->setField('t_status',1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    public function down(){
        $new_id = input('id');
        $res = Db::table('up_types')->where('t_id',$new_id)->setField('t_status',2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 编辑
     * @return mixed
     */
    public function imageSet(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $ppssss = Db::table('up_pp')->where('pp_t_id',$t_id)->column('pp_id');
            $all = [];
            $pids = [];
            if($pps){
                foreach ($pps as $k => $v){
                    $all[] = [
                        'pb_pp_id' => $v,
                        'pb_url' => $t_pp,
                        'pb_sort' => 1,
                        'pb_where' => 2,
                        'pb_time' => $today
                    ];
//                    $pids[] = $v['pp_id'];
                }
                Db::table('up_pp_banner')
                    ->where('pb_pp_id','in',$pps)
                    ->where('pb_where',2)
                    ->delete();
                Db::table('up_pp_banner')->insertAll($all);
            }else{
                Db::table('up_pp_banner')
                    ->where('pb_pp_id','in',$ppssss)
                    ->where('pb_where',2)
                    ->delete();
            }
            $data = [
                't_pp' => $t_pp,
                't_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_types')->where('t_id',$t_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $t_id = input('t_id');
        $info = Db::name('types')
            ->where('t_id',$t_id)
            ->find();
        $pp_array = Db::table('up_pp_banner')
            ->where('pb_where',2)
            ->column('pb_pp_id');
        $pps = Db::name('pp')
            ->where('pp_t_id',$t_id)
            ->field('pp_id,pp_name')
            ->order('pp_sort desc')
            ->select();
        $this->assign([
            'info' => $info,
            'pp_array' => $pp_array,
            'pps' => $pps
        ]);
        return $this->fetch('types/banneradd');
    }
    /**
     * 设置优惠信息
     * @return mixed
     */
    public function couponSet(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');

            if($pps){
                Db::table('up_pp')
                    ->where('pp_id','in',$pps)
                    ->update([
                        'pp_coupon1' => $t_pp_coupon1,
                        'pp_coupon2' => $t_pp_coupon2,
                        'pp_coupon3' => $t_pp_coupon3,
                        'pp_coupon4' => $t_pp_coupon4,
                        'pp_time' => $today
                    ]);
            }else{
                $ppssss = Db::table('up_pp')->where('pp_t_id',$t_id)->column('pp_id');
                Db::table('up_pp')
                    ->where('pp_id','in',$ppssss)
                    ->update([
                        'pp_coupon1' => '',
                        'pp_coupon2' => '',
                        'pp_coupon3' => '',
                        'pp_coupon4' => '',
                        'pp_time' => $today
                    ]);
            }
            $data = [
                't_pp_coupon1' => $t_pp_coupon1,
                't_pp_coupon2' => $t_pp_coupon2,
                't_pp_coupon3' => $t_pp_coupon3,
                't_pp_coupon4' => $t_pp_coupon4,
                't_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_types')->where('t_id',$t_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $t_id = input('t_id');
        $info = Db::name('types')
            ->where('t_id',$t_id)
            ->find();
        $pps = Db::name('pp')
            ->where('pp_t_id',$t_id)
            ->field('pp_id,pp_name')
            ->order('pp_sort desc')
            ->select();
        $pp_array = Db::table('up_pp')
            ->where('pp_coupon1','')
            ->whereOr('pp_coupon1',null)
            ->column('pp_id');
        $pp_array = Db::table('up_pp')
            ->where('pp_id','not in',$pp_array)
            ->column('pp_id');
        $this->assign([
            'info' => $info,
            'pp_array' => $pp_array,
            'pps' => $pps
        ]);
        return $this->fetch('types/coupon');
    }
    /**
     * 设置优惠信息
     * @return mixed
     */
    public function yongjinSet(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');

            if($pps){
                Db::table('up_pp')
                    ->where('pp_id','in',$pps)
                    ->update([
                        'pp_yongjin1' => $t_pp_yongjin1,
                        'pp_yongjin2' => $t_pp_yongjin2,
                        'pp_yongjin3' => $t_pp_yongjin3,
                        'pp_yongjin4' => $t_pp_yongjin4,
                        'pp_time' => $today
                    ]);
            }else{
                $ppssss = Db::table('up_pp')->where('pp_t_id',$t_id)->column('pp_id');
                Db::table('up_pp')
                    ->where('pp_id','in',$ppssss)
                    ->update([
                        'pp_yongjin1' => '',
                        'pp_yongjin2' => '',
                        'pp_yongjin3' => '',
                        'pp_yongjin4' => '',
                        'pp_time' => $today
                    ]);
            }
            $data = [
                't_pp_yongjin1' => $t_pp_yongjin1,
                't_pp_yongjin2' => $t_pp_yongjin2,
                't_pp_yongjin3' => $t_pp_yongjin3,
                't_pp_yongjin4' => $t_pp_yongjin4,
                't_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_types')->where('t_id',$t_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $t_id = input('t_id');
        $info = Db::name('types')
            ->where('t_id',$t_id)
            ->find();
        $pps = Db::name('pp')
            ->where('pp_t_id',$t_id)
            ->field('pp_id,pp_name')
            ->order('pp_sort desc')
            ->select();
        $pp_array = Db::table('up_pp')
            ->where('pp_yongjin1','')
            ->whereOr('pp_yongjin1',null)
            ->column('pp_id');
        $pp_array = Db::table('up_pp')
            ->where('pp_id','not in',$pp_array)
            ->column('pp_id');
        $this->assign([
            'info' => $info,
            'pp_array' => $pp_array,
            'pps' => $pps
        ]);
        return $this->fetch('types/yongjin');
    }
    /**
     * 批量设置图片
     * @return mixed
     */
    public function imageSet2(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            if($type_img){
                $res = Db::table('up_pp_banner')->alias('s')
                    ->join('up_pp pp','s.pb_pp_id=pp.pp_id','LEFT')
                    ->join('up_types t','pp.pp_t_id=t.t_id','LEFT')
                    ->where('pb_where',2)
                    ->where('t_id','in',$t_id)
                    ->update([
                        'pb_url' => $type_img,
                        'pb_time' => $today
                    ]);
            }else{
                $res = Db::table('up_pp_banner')->alias('s')
                    ->join('up_pp pp','s.pb_pp_id=pp.pp_id','LEFT')
                    ->join('up_types t','pp.pp_t_id=t.t_id','LEFT')
                    ->where('pb_where',2)
                    ->where('t_id','in',$t_id)
                    ->delete();
            }
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $t_id = input('t_id');
        $this->assign([
            't_id' => $t_id
        ]);
        return $this->fetch('types/alltypeimage');
    }
    /**
     * 批量设置佣金图片
     * @return mixed
     */
    public function yongjinimgSet2(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            if($type_img){
                $res = Db::table('up_pp_banner')->alias('s')
                    ->join('up_pp pp','s.pb_pp_id=pp.pp_id','LEFT')
                    ->join('up_types t','pp.pp_t_id=t.t_id','LEFT')
                    ->where('pb_where',9)
                    ->where('t_id','in',$t_id)
                    ->update([
                        'pb_url' => $type_img,
                        'pb_time' => $today
                    ]);
            }else{
                $res = Db::table('up_pp_banner')->alias('s')
                    ->join('up_pp pp','s.pb_pp_id=pp.pp_id','LEFT')
                    ->join('up_types t','pp.pp_t_id=t.t_id','LEFT')
                    ->where('pb_where',9)
                    ->where('t_id','in',$t_id)
                    ->delete();
            }
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $t_id = input('t_id');
        $this->assign([
            't_id' => $t_id
        ]);
        return $this->fetch('types/alltypeimage2');
    }
    /**
     * 批量设置优惠信息
     * @return mixed
     */
    public function couponSet2(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $res = Db::table('up_pp pp')
                ->where('pp_t_id','in',$t_id)
                ->update([
                    'pp_coupon1' => $t_pp_coupon1,
                    'pp_coupon2' => $t_pp_coupon2,
                    'pp_coupon3' => $t_pp_coupon3,
                    'pp_coupon4' => $t_pp_coupon4,
                    'pp_time' => $today
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $t_id = input('t_id');
        $this->assign([
            't_id' => $t_id
        ]);
        return $this->fetch('types/alltypecoupon');
    }
    /**
     * 批量设置优惠信息
     * @return mixed
     */
    public function yongjinSet2(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $res = Db::table('up_pp pp')
                ->where('pp_t_id','in',$t_id)
                ->update([
                    'pp_yongjin1' => $t_pp_yongjin1,
                    'pp_yongjin2' => $t_pp_yongjin2,
                    'pp_yongjin3' => $t_pp_yongjin3,
                    'pp_yongjin4' => $t_pp_yongjin4,
                    'pp_time' => $today
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $t_id = input('t_id');
        $this->assign([
            't_id' => $t_id
        ]);
        return $this->fetch('types/alltypeyongjin');
    }
}